/**
 * 动态加载js文件工具
 */
define(['jqueryUtils'], function () {
	function Route(baseViewPath){
		this.baseViewPath = baseViewPath;
	}
	
	Route.prototype = {
		constructor: Route,
		setBaseViewPath: function(value){
			this.baseViewPath = value;
		},
		setControllerProvider:function(value){
			this.$controllerProvider = value;
		},
        setCompileProvider:function(value){
        	this.$controllerProvider = value;
        },
        setStateProvider: function(value){
        	this.$stateProvider = value;
        },
        state : function(route,  methods){
        	var self = this;
        	route = angular.extend({stateName:null, controller:null, path:null}, route);
        	//var url = "/"+route.stateName;
        	//this.$stateProvider.state(route.stateName, {url:url, views:{}});
        	var folder = route.stateName;   // 默认 存储路径为  状态名
        
        	if(route.path){
        		folder = route.path;
        	}
        	
        	angular.forEach(methods, function(method){
        		
        		var mStateName = method.name;
        		if(method.name.indexOf("?")>0){
        			mStateName = method.name.substring(0, method.name.indexOf("?"));
        		}
        		var stateName = route.stateName + "." + mStateName;
        		/**name 即是 url*/
        		var url="/"+method.name;
        		/**模板文件路径*/
        		var templateUrl = self.baseViewPath + "/" + folder +"/" + mStateName+".html";
        		if(method.templateUrl){
        			templateUrl = self.baseViewPath + "/" + folder +"/" + method.templateUrl;
        			
        			if(angular.isFunction(method.templateUrl)){ //如果是函数 直接赋值
        				templateUrl = method.templateUrl;
        			}
        		}
        		/**设置ctrl文件*/
        		var controllerName = route.controller;
        		if(method.controller){
        			controllerName = method.controller;
        		}
        		/**设置ctrl名称*/
        		var methodName = mStateName;
        		if(mStateName.indexOf("_")>0){	  // 转化为驼峰式命名
        			
        			var temps = mStateName.split('_');
        			
        			angular.forEach(temps, function(temp, i){
        				if(i==0){
        					methodName = temp;
        				}else{
        					methodName += temp.substring(0,1).toUpperCase()+temp.substring(1);
        				}
        			});
        		}
        		if(method.mName){
        			methodName = method.mName;
        		}
        		var option = {url: url, templateUrl:templateUrl, controllerName:controllerName, method:methodName};
        		self.$stateProvider.state(stateName,config(option, self));
        	});
        	
        	return this;
        },
        config: function(option){
        	return config(option, this);
        }
	};
	
    
    function config(option, route) {
    	option = angular.extend({controller:null, method:null}, option);
        var defer,
            routeDefinition = {};
        
        routeDefinition.url = option.url;
        	
    	routeDefinition.templateUrl = option.templateUrl;
    	routeDefinition.controller = option.method;   //控制器名称
        routeDefinition.resolve = {
            delay:function ($q) {
                defer = $q.defer();
                var dependencies = [option.controllerName];
                require(dependencies, function (controller) {
                	if(option.controllerName){
                		route.$controllerProvider.register(option.method, controller[option.method]);
                	}
                    defer.resolve();
                });
                return defer.promise;
            }
        };
       
        return routeDefinition;
    }

    return new Route();
});
